package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.text.html.HTMLDocument.Iterator;

import pracownik.Pracownik;
import sop.Main;

import net.miginfocom.swing.MigLayout;




public class MenuLeft extends JPanel implements ActionListener {
	static final JTabbedPane menuTab = new JTabbedPane();
	static final JPanel	menuPanel    = new JPanel();
	/*
	 * Buttony
	 */
	static final MenuButton witamy = new MenuButton("Witamy");
	static final MenuButton pracownicy = new MenuButton("Pracownicy");
	static final MenuButton zadanie = new MenuButton("Zadania");
	static final MenuButton dane = new MenuButton("Moje Dane");
	static final MenuButton pobierzZadania = new MenuButton("Pobierz zadania");
	MenuLeft() {
		add(menuTab);
		
		
		MigLayout layout = new MigLayout();
		menuTab.add(menuPanel, "Menu");
		menuPanel.setLayout(layout);
		this.setPreferredSize(new Dimension(200,600));
		
		witamy.addActionListener(this);
		pracownicy.addActionListener(this);
		zadanie.addActionListener(this);
		dane.addActionListener(this);
		pobierzZadania.addActionListener(this);
		
		menuPanel.add(witamy, "wrap 15px");
		if(login.kto()){menuPanel.add(pracownicy, "wrap 15px");}
		else{
		    menuPanel.add(dane, "wrap 15px"); 
		}
		menuPanel.add(zadanie, "wrap 15px");
		if(!login.kto()) {
		    menuPanel.add(pobierzZadania);   
		}
		
	}
	
	private int buttonNum = 0;
	
	MenuLeft(int buttonNum) {
	    this.buttonNum = buttonNum;
	}
	
	ArrayList<PagePracownik> workersPages = new ArrayList<PagePracownik>();
	ArrayList<Page> pages = new ArrayList<Page>();
	@Override
	public void actionPerformed(ActionEvent e) {
	    PageWelcome pageWelcome;
	    PagePracownicy pagePracownicy;
	    PageZadanie pageZadanie;
	    synchronized (this.getTreeLock()) { 
	        pageWelcome = PageWelcome.getInstance();
	    }
	    synchronized (this.getTreeLock()) { 
	        pagePracownicy = PagePracownicy.getInstance();
	    }
	    synchronized (this.getTreeLock()) { 
	        pageZadanie = PageZadanie.getInstance();    
	    }
	    if (buttonNum == 0) {

	            
	        
	        pages.add(pageWelcome);
	       if(login.kto()) pages.add(pagePracownicy);
	        pages.add(pageZadanie);
	         
	        
	        if (e.getSource() == witamy) {
	          //  checkAndAddToTemplate(pageWelcome);
	            pageWelcome.visible(true);
	            pagePracownicy.visible(false);
	            pageZadanie.visible(false);
	        } else if (e.getSource() == pracownicy) {
	          //  checkAndAddToTemplate(pagePracownicy);
	            pageWelcome.visible(false);
	            pagePracownicy.visible(true);
	            pageZadanie.visible(false); 
	        } else if (e.getSource() == zadanie) {
	              pageWelcome.visible(false);
	              pagePracownicy.visible(false);
	              pageZadanie.visible(true);   
	           // changeVisibility(pageZadanie);
	        }else if(e.getSource() == dane){
	            try {
	                ResultSet rs = Main.db.select("idPracownik",
	                        "Pracownik where login='"+login.user+"'", false)   ; 
	                        rs.next();
	                        
                    new PracownikEdit(rs.getInt("idPracownik"));
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
	        }  else if (e.getSource() == pobierzZadania) {
               PageZadanie.refreshShowTasks();
          }
	        
	    } else {
           
	    }
	    
		
	}




}
